package action;

import Impl.ReplyDaoImpl;
import Impl.plateDaoImpl;
import entity.Admin;
import entity.Plate;
import entity.ReplyInfo;
import entity.TopicInfo;
import entity.page.DetailPage;
import entity.page.ListPage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ReplyOpt")
public class ReplyOpt extends HttpServlet {
    ReplyDaoImpl replyDao = new ReplyDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("--------------正在执行ReplyOpt------------");
        String method = request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        if (method!=null&&method.equals("del")){
            System.out.println("正在删除Topic:");
            int id = Integer.parseInt(request.getParameter("id"));
            replyDao.deleteReplyById(id);
        }else if (method!=null && method.equals("superadmin")){
            int pageNow = 0;
            if (request.getParameter("pageNow") == null) {
                pageNow = 1;
            } else {
                pageNow = Integer.parseInt(request.getParameter("pageNow"));
            }
            ArrayList<ReplyInfo> replyInfos = replyDao.findAllReply();
            int allcount = replyInfos.size();
            int allpage;
            if (allcount % 5 == 0) {
                allpage = allcount / 5;
            } else {
                allpage = allcount / 5 + 1;
            }
            request.setAttribute("allcount", allcount);
            request.setAttribute("allpage", allpage);
            request.setAttribute("pageNow", pageNow);
            request.setAttribute("replyInfos", replyInfos);
            request.getRequestDispatcher("/super-reply-list.jsp").forward(request, response);
        } else {
            int pageNow = 0;
            if (request.getParameter("pageNow") == null) {
                pageNow = 1;
            } else {
                pageNow = Integer.parseInt(request.getParameter("pageNow"));
            }
            Admin admin = (Admin) request.getSession().getAttribute("admin");
            String condition = admin.getIdentity();
            System.out.println("管理员身份：" + condition);
            plateDaoImpl plateDao = new plateDaoImpl();
            List<Plate> tid = plateDao.findPlateByCondition(condition);
            List<Plate> childPlate = plateDao.findPlateByPId(tid.get(0).getSid());
            System.out.println(tid.get(0).getSid());
            ArrayList<ReplyInfo> replyInfos = replyDao.findReplyInfo(tid.get(0).getSid());
            for (Plate t :childPlate){
                ArrayList<ReplyInfo> replyInfo = replyDao.findReplyInfo(t.getSid());
                replyInfos.addAll(replyInfo);
            }
            int allcount = replyInfos.size();
            int allpage;
            if (allcount % 5 == 0) {
                allpage = allcount / 5;
            } else {
                allpage = allcount / 5 + 1;
            }
            request.setAttribute("allcount", allcount);
            request.setAttribute("allpage", allpage);
            request.setAttribute("pageNow", pageNow);
            request.setAttribute("replyInfos", replyInfos);
            request.getRequestDispatcher("/reply-list.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
